<template>
  <div>
    <vxe-toolbar id="toolbarId" :setting="{ storage: true }" export :refresh="{ query: findList }">
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
        <el-button @click="funcAdd">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      show-footer
      highlight-hover-row
      row-id="id"
      class="mytable-footer"
      height="700"
      :loading="loading"
      :footer-method="footerMethod"
      :data="tableData"
      remote-sort
      @sort-change="sortList"
    >
      <vxe-table-column>
        <vxe-table-column
          field="authStatus"
          title="审核状态"
          show-header-overflow
          show-overflow
          min-width="130"
          :formatter="formatterAuthStatus"
          sortable
        />
        <template v-slot:header>
          <el-select v-model="listQuery.authStatus" placeholder="请选择" clearable>
            <el-option
              v-for="item in authStatusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="linkMan"
          title="车队名"
          show-header-overflow
          show-overflow
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.linkMan" placeholder="请输入车队名" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="phone"
          title="手机"
          show-header-overflow
          show-overflow
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.phone" placeholder="请输入手机" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="identificationNumber"
          title="身份证号"
          show-header-overflow
          show-overflow
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.identificationNumber" placeholder="请输入身份证号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="balance"
          title="余额"
          show-header-overflow
          show-overflow
          min-width="200"
          sortable
        />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.balanceStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.balanceEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtCreated"
          min-width="420"
          title="注册时间"
          show-header-overflow
          show-overflow
          sortable
          :resizable="false"
        />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtCreatedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeDate"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作" min-width="150" fixed="right">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="详情" placement="top">
            <vxe-button type="text" @click="funcDetail(scope.row)">
              <i class="el-icon-document" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row)">
              <i class="el-icon-edit" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.partyCode)">
              <i class="el-icon-delete" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
  </div>
</template>
<script>
import Vue from "vue";
import "xe-utils";
import VXETable from "vxe-table";
import "vxe-table/lib/index.css";
import XEUtils from "xe-utils";
import VXEUtils from "vxe-utils";
Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ["cookie"] });
import { getDriverTeamList, delDriverTeam } from "@/api/driver";
export default {
  name: "DriverTeamList",
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      loading: false,
      tableData: [],
      gmtCreatedRange: "",
      listQuery: {
        driverType: 1,
        authStatus: "",
        linkMan: "",
        phone: "",
        identificationNumber: "",
        balanceStart: "",
        balanceEnd: "",
        gmtCreatedEnd: "",
        gmtCreatedStart: "",
        pageNum: 1,
        pageSize: 10,
        sortField: "",
        sortType: "",
        total: 0
      },
      sumObject: {
        balance: 0
      },
      // 审核状态
      authStatusOptions: [
        { label: "未审核", value: 0, color: "gray" },
        { label: "审核中", value: 1, color: "#0000ff" },
        { label: "审核未通过", value: 2, color: "#ff0000" },
        { label: "审核通过", value: 3, color: "#00bd93" }
      ]
    };
  },
  created() {
    this.findList();
  },
  methods: {
    findList() {
      this.loading = true;
      return new Promise(resolve => {
        getDriverTeamList(this.listQuery).then(response => {
          this.tableData = response.data.records;
          this.listQuery.total = response.data.total;
          this.sumObject = response.data.sumObject;
          this.loading = false;
          resolve();
        });
      })
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 排序
    sortList({ column, property, order }) {
      this.listQuery.sortField = property;
      this.listQuery.sortType = order;
      this.findList();
    },
    // 添加
    funcAdd() {
      this.$router.push({ 
        path: "/basicInfo/driver/addDriverTeamForm",
      });
    },
    //详情
    funcDetail(row) {
      this.$router.push({
        path: "/basicInfo/driver/infoDriverTeamForm",
        query: { code: row.code, partyCode: row.partyCode }
      });
    },
    // 编辑
    funcEdit(row) {
      this.$router.push({
        path: "/basicInfo/driver/editDriverTeamForm",
        query: { code: row.code, partyCode: row.partyCode }
      });
    },
    // 删除方法
    funcDelete(codes) {
      this.$confirm("确认删除吗？")
        .then(_ => {
          delDriverTeam({ codes }).then(response => {
            this.$message({ message: response.msg, type: "success" });
            this.findList();
          });
        })
        .catch(_ => {});
    },
    // 翻译审核状态
    formatterAuthStatus({ cellValue }) {
      const item = this.authStatusOptions.find(
        item => item.value === cellValue
      );
      return item ? item.label : "";
    },
    changeDate(value) {
      if (value) {
        this.listQuery.gmtCreatedStart = value[0] + " 00:00:00";
        this.listQuery.gmtCreatedEnd = value[1] + " 23:59:59";
      } else {
        this.listQuery.gmtCreatedStart = "";
        this.listQuery.gmtCreatedEnd = "";
      }
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage;
      this.listQuery.pageSize = pageSize;
      this.findList();
    },/* 
    // 底部颜色设置
    footerCellClassName({ $rowIndex, column, columnIndex }) {
      if (columnIndex === 0) {
        if ($rowIndex === 0) {
          return "col-blue";
        } else {
          return "col-red";
        }
      }
    }, */
    // 设置底部合计
    footerMethod({ columns, data }) {
      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return "合计";
          }
          if (columnIndex === 5) {
            // 余额
            return this.sumObject.balance;
          }
          if (["pageviews", "forecast"].includes(column.property)) {
            return XEUtils.sum(data, column.property);
          }
          return null;
        })
      ];
    }
  }
};
</script>

<style>
.vxe-table .vxe-body--column:not(.col--ellipsis),
.vxe-table .vxe-footer--column:not(.col--ellipsis),
.vxe-table .vxe-header--column:not(.col--ellipsis) {
  padding: 6px 0;
}
.vxe-toolbar {
  padding: 0 0 10px 0;
}
</style>
